﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
namespace MVC4WPF.Automation.Dialog
{
    public static class DialogHelper
    {
        /// <summary>
        /// Determines if the name of the code file is correctly formatted. 
        /// If it is not formatted correctly a messagebox will display the requiremetns for the name. 
        /// </summary>
        /// <param name="name">The name to be used a the file name and the class name.</param>
        /// <param name="nameType">The type of name being geneterated.this is used in the error message displayed to the user.</param>
        /// <returns>True the name is formatted correctly, 
        /// False the name is incorrectly formatted and a messagebox has been raised to the user to fix the name.</returns>
        public static bool CodeNameCheck(string name, string nameType)
        {

            bool nameIsIncorrect = Regex.IsMatch(name, @"CON|AUX|PRN|(COM\d)|LPT\d|[\.]\Z|\.\.|([^\.\w])");

            if (nameIsIncorrect)
            {
                StringBuilder formatMessage = new StringBuilder();

                formatMessage.AppendLine(string.Format("The name of the {0} is incorrectly formatted.", nameType));
                formatMessage.AppendLine(string.Format("The {0} cannot have the characters /?:&\\*\"<>|#%@!^&=+ in the name itself.", nameType));
                formatMessage.AppendLine("The name cannot be . or ..");
                formatMessage.AppendLine("The name must start with a standard letter or an underscore, no extended characters allowed.");
                formatMessage.AppendLine("The name cannot end with a .");
                formatMessage.AppendLine("The name cannot be empty.");

                MessageBox.Show(formatMessage.ToString(), "Incorrect format", MessageBoxButton.OK, MessageBoxImage.Exclamation);

                return false;
            }
            return true;
        }

        /// <summary>
        /// Check function that determines if the code file name is correctly formatted
        /// </summary>
        /// <param name="name">The code file name to check</param>
        /// <returns>True if the name can be saved. False if the name is incorrectly formatted</returns>
        public static bool CodeNameCanSave(string name)
        {
            if (name == null | name == string.Empty)
            {
                return false;
            }

            bool nameIncorrect = Regex.IsMatch(name, @"\A[\W]|\A\.|\A[\d]|\.\Z| ");

            if (nameIncorrect)
            {
                return false;
            }

            return true;

        }

       
    }
}
